package news;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import user.User;

/**
 * Servlet implementation class ServletNewsAdd
 */
@WebServlet("/ServletNewsAdd")
public class ServletNewsAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletNewsAdd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		News news = new News();
		news.setTitle(request.getParameter("title"));
		news.setContent(request.getParameter("content"));
		news.setUid(((User)request.getSession().getAttribute("user")).getUid());
		Timestamp pubtime = new Timestamp(new Date().getTime());
		news.setPubtime(pubtime);
		Newsdao newsdao = new Newsdao();
		int result = newsdao.doNewsAdd(news);
		if(result >= 0) {
			response.getWriter().print("<script type=\"text/javascript\">alert(\"发布成功！\");window.location=\"/Lesson_310/news/news_list.jsp\";</script>");
			return;
		}else {
			response.sendRedirect("/Lesson_310/news/news_success.jsp?count=1");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
